/**
 * @author LARAKI Meryem, MATHIEU Renaud
 * <h1>ESIAL 2A - PIDR 2010 </h1>
 * <h2>Modelisation de connaissances et competences informatiques</h2>
 */

package graphique;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import modele.Parser;

import org.semanticweb.owlapi.model.OWLOntologyCreationException;

public class VueChargerOntologie extends JFrame{
	private static final long serialVersionUID = 1L;
	
	//--------------------
	// Attributs
	//--------------------
	public JButton charger;
	public JTextField fichierCharge;
	public JButton imageAccueil;
	
	//--------------------
	// Constructeurs
	//--------------------
	public VueChargerOntologie(){
		super("PIDR : Laraki-Mathieu");
		
		this.setLayout(new BorderLayout());
		
		// Instanciation des elements graphiques
		charger = new JButton("Charger une ontologie");
		fichierCharge = new JTextField("version1.owl",20);
		imageAccueil = new JButton(new ImageIcon(this.getClass().getClassLoader().getResource("pidr_accueil.png")));
		
		// Ajout des elements graphiques
		this.add(fichierCharge, BorderLayout.NORTH);
		this.add(charger, BorderLayout.SOUTH);
		this.add(imageAccueil, BorderLayout.CENTER);
		
		// Actions
		charger.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	
            	JFileChooser chooser = new JFileChooser();
            	chooser.setApproveButtonText("Charger l'ontologie (uniquement .owl )");
            	chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            	chooser.setFileFilter(new OwlFilter());
            	int status = chooser.showOpenDialog(null);
 
                if (status == JFileChooser.APPROVE_OPTION) {
                	fichierCharge.setText(chooser.getSelectedFile().getPath());
                }
            }
        });
		
		imageAccueil.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	
            	File leFichier = new File(fichierCharge.getText());
    			if(leFichier.isFile()){
    				
    				// Lancement de l'application
    				try {
						Parser parser = new Parser(leFichier);
						new IGLink(parser);
						
					} catch (OWLOntologyCreationException e1) { System.out.println("Le fichier .owl incorrect"); }
    				// Fermeture de la fenetre de chargement
    				setVisible(false);
    	            dispose();
    			}else{
    				JOptionPane.showMessageDialog(null,"Le fichier .owl incorrect","Attention",JOptionPane.WARNING_MESSAGE);
    			}
            }
        });
		
		// Options
		fichierCharge.setHorizontalAlignment(JTextField.CENTER);
		
		// Options de la JFrame
		pack();
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}//cons-1
	
	//--------------------
	// Methodes
	//--------------------
	public class OwlFilter extends FileFilter {

	    public boolean accept(File f) {
	        if (f.isDirectory())
	            return true;
	        else 
	        	return (getExtension(f).compareTo("owl") == 0);
	    }
	    
	    public String getDescription() {
	        return ".owl";
	    }
	    
	    public String getExtension(File f) {
	        String ext = null;
	        String s = f.getName();
	        int i = s.lastIndexOf('.');

	        if (i > 0 &&  i < s.length() - 1) {
	            ext = s.substring(i+1).toLowerCase();
	        }
	        return ext;
	    }
	}
}
